package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author: jwf
 * @date ：Created in 2021/3/12
 * @description ：
 * @version: 1.0
 */

@RestController
@RequestMapping("menu")
@Slf4j
public class MenuController {

    @DubboReference
    private MenuService menuService;

    @GetMapping("findPage")
    @PreAuthorize("hasAnyAuthority('MENU_QUERY')")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("[菜单管理---分页查询]data: {}", queryPageBean);
        PageResult pageResult = menuService.findPage(queryPageBean);
        return new Result(true, MessageConst.GET_MENU_SUCCESS,pageResult);
    }

    @PostMapping("add")
    public Result add(@RequestBody Menu menu){
        log.info("[菜单---新增]data: {}", menu);
        menuService.add(menu);
        return new Result(true,MessageConst.ADD_MENU_SUCCESS);
    }
    @PostMapping("edit")
    public Result edit(@RequestBody Menu menu){
        log.info("[菜单-根据名称修改数据]data: {}", menu);
        menuService.edit(menu);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }
    @GetMapping("findById")
    public Result findById(Integer id){
        log.info("[菜单-根据id查询]data: {}", id );
        Menu menu = menuService.findById(id);
        return new Result(true,MessageConst.QUERY_menu_SUCCESS,menu);
    }
    @PostMapping("delete")
    public Result deleteById(Integer id){
        log.info("[菜单-根据id删除]data: {}", id );
        menuService.deleteById(id);
        return new Result(true,MessageConst.DELETE_menu_SUCCESS);
    }
}
